package cn.iocoder.yudao.module.contract.dal.mysql.invoiceContract;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.contract.controller.admin.orderContract.vo.OrderContractPageRespVO;
import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.invoice.InvoiceDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.invoiceContract.InvoiceContractDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import cn.iocoder.yudao.module.contract.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.contract.controller.admin.invoiceContract.vo.*;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.units.qual.C;

/**
 * 发票合同 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface InvoiceContractMapper extends BaseMapperX<InvoiceContractDO> {

    default PageResult<InvoiceContractPageRespVO> selectPage(InvoiceContractPageReqVO reqVO) {
        return selectJoinPage(reqVO,InvoiceContractPageRespVO.class, new MPJLambdaWrapperX<InvoiceContractDO>()
                .betweenIfPresent(InvoiceContractDO::getGeneratedTime, reqVO.getGeneratedTime())
                .eqIfPresent(InvoiceContractDO::getInvoiceMonth, reqVO.getInvoiceMonth())
                .eqIfPresent(InvoiceContractDO::getGenerationType, reqVO.getGenerationType())
                .selectAll(InvoiceContractDO.class) //
                .selectAssociation(ClientDO.class, InvoiceContractPageRespVO::getClientDO) //
                .in(!ObjectUtils.isEmpty(reqVO.getXfmcList()),InvoiceContractDO::getXfmc, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getXfmcList())?reqVO.getXfmcList():new Object[]{""}))
                .like(!ObjectUtils.isEmpty(reqVO.getGfmc()) && "1".equals(reqVO.getGfmcType()),InvoiceContractDO::getGfmc, reqVO.getGfmc())
                .in(!ObjectUtils.isEmpty(reqVO.getGfmc()) && "2".equals(reqVO.getGfmcType()),InvoiceContractDO::getGfmc, !ObjectUtils.isEmpty(reqVO.getGfmc()) ?reqVO.getGfmc().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getDdh()) && "1".equals(reqVO.getDdhType()),InvoiceContractDO::getDdh, reqVO.getDdh())
                .in(!ObjectUtils.isEmpty(reqVO.getDdh()) && "2".equals(reqVO.getDdhType()),InvoiceContractDO::getDdh, !ObjectUtils.isEmpty(reqVO.getDdh()) ?reqVO.getDdh().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getContractNo()) && "1".equals(reqVO.getContractNoType()),InvoiceContractDO::getContractNo, reqVO.getContractNo())
                .in(!ObjectUtils.isEmpty(reqVO.getContractNo()) && "2".equals(reqVO.getContractNoType()),InvoiceContractDO::getContractNo, !ObjectUtils.isEmpty(reqVO.getContractNo()) ?reqVO.getContractNo().split(","): new Object[]{""})

                .in(!ObjectUtils.isEmpty(reqVO.getContractTypeList()),InvoiceContractDO::getContractType, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getContractTypeList())?reqVO.getContractTypeList():new Object[]{""}))
                .in(!ObjectUtils.isEmpty(reqVO.getVsbedList()),InvoiceContractDO::getVsbed, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getVsbedList())?reqVO.getVsbedList():new Object[]{""}))

//                .and("1".equals(reqVO.getIsSignIn()),i -> i.isNotNull(InvoiceContractDO::getFileIds).ne(InvoiceContractDO::getFileIds, ""))
//                .and("0".equals(reqVO.getIsSignIn()),i -> i.isNull(InvoiceContractDO::getFileIds).or().eq(InvoiceContractDO::getFileIds, ""))

                .in(!ObjectUtils.isEmpty(reqVO.getStatusList()),InvoiceContractDO::getStatus, Arrays.asList(!ObjectUtils.isEmpty(reqVO.getStatusList())?reqVO.getStatusList():new Object[]{""}))

                .like(!ObjectUtils.isEmpty(reqVO.getQyusername()) && "1".equals(reqVO.getQyusernameType()),InvoiceContractDO::getQyusername, reqVO.getQyusername())
                .in(!ObjectUtils.isEmpty(reqVO.getQyusername()) && "2".equals(reqVO.getQyusernameType()),InvoiceContractDO::getQyusername, !ObjectUtils.isEmpty(reqVO.getQyusername()) ?reqVO.getQyusername().split(","): new Object[]{""})

                .like(!ObjectUtils.isEmpty(reqVO.getSalesmethodname()) && "1".equals(reqVO.getSalesmethodnameType()),InvoiceContractDO::getSalesmethodname, reqVO.getSalesmethodname())
                .in(!ObjectUtils.isEmpty(reqVO.getSalesmethodname()) && "2".equals(reqVO.getSalesmethodnameType()),InvoiceContractDO::getSalesmethodname, !ObjectUtils.isEmpty(reqVO.getSalesmethodname()) ?reqVO.getSalesmethodname().split(","): new Object[]{""})

                .and("1".equals(reqVO.getRecycleStatus()),i->i.isNull(InvoiceContractDO::getFileIds).or().eq(InvoiceContractDO::getFileIds,""))
                .and("1".equals(reqVO.getRecycleStatus()),i->i.isNull(InvoiceContractDO::getScanFileIds).or().eq(InvoiceContractDO::getScanFileIds,""))
                .and("2".equals(reqVO.getRecycleStatus()),i->i.isNull(InvoiceContractDO::getFileIds).or().eq(InvoiceContractDO::getFileIds,""))
                .and("2".equals(reqVO.getRecycleStatus()),i->i.isNotNull(InvoiceContractDO::getScanFileIds).ne(InvoiceContractDO::getScanFileIds,""))
                .and("3".equals(reqVO.getRecycleStatus()),i->i.isNotNull(InvoiceContractDO::getFileIds).ne(InvoiceContractDO::getFileIds,""))

                .leftJoin(ClientDO.class, ClientDO::getClientCode, InvoiceContractDO::getClientCode)
                .in(!ObjectUtils.isEmpty(reqVO.getZzhtgklx()),ClientDO::getZzhtgklx,  !ObjectUtils.isEmpty(reqVO.getZzhtgklx()) ?reqVO.getZzhtgklx().split(","): new Object[]{""})

                .orderByDesc(ObjectUtils.isEmpty(reqVO.getOrder()), InvoiceContractDO::getCreateTime)
                .orderBy(ObjectUtils.isNotEmpty(reqVO.getOrder()),"ascending".equals(reqVO.getOrder()), StringUtils.toUnderScoreCase(reqVO.getProp()))
        );
    }
    @Select("SELECT count(1)  FROM ct_invoice_contract  WHERE YEAR ( create_time ) = YEAR ( CURDATE( ) ) AND MONTH ( create_time ) = MONTH ( CURDATE( ) ) and companyid = #{companyid};")
    long countCompanyByMonth(String companyid);
}